package com.android.lovely.ui.dialog

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.graphics.Color
import android.os.Bundle
import android.text.method.LinkMovementMethod
import android.view.WindowManager
import android.widget.FrameLayout
import android.widget.TextView
import androidx.core.graphics.toColorInt
import androidx.core.view.isVisible
import com.android.lovely.R
import com.android.lovely.databinding.DialogLovelyBinding
import com.kotlin.extensions.view.ViewExt.clickable

/**
 * 自定义对话框类，使用建造者模式构建对话框。
 *
 * @property context 上下文对象
 */

fun Context.showLovelyDialog(block: LovelyDialog.Builder.() -> LovelyDialog.Builder) =
    LovelyDialog.Builder(this).apply {
        block.invoke(this@apply)
    }.build().show()

class LovelyDialog private constructor(private val context: Context) :
    Dialog(context, R.style.Lovely_Dialog) {
    private lateinit var dialogBinding: DialogLovelyBinding
    private var dialogTitle: CharSequence? = null
    private var dialogMessage: CharSequence? = null
    private var isOnlyPositiveButton: Boolean = false
    private var showTopClose: Boolean = false
    private var showBottomClose: Boolean = false
    private var showTopImage: Boolean = false
    private var isSpan: Boolean = false
    private var topImage: Int = R.drawable.icon_green
    private var negativeButtonText: CharSequence = context.getString(R.string.cancel)
    private var negativeButtonTextColor: Int = context.getColor(R.color.blue_7782A7)
    private var negativeButtonColor: Int = "#D1D1FF".toColorInt()
    private var negativeButtonClickListener: ((Dialog) -> Unit)? = null
    private var positiveButtonText: CharSequence = context.getString(R.string.confirm)
    private var positiveButtonTextColor: Int = Color.WHITE
    private var positiveButtonColor: Int = context.getColor(R.color.color_main)
    private var positiveButtonClickListener: ((Dialog) -> Unit)? = null
    private var onDismissListener: (() -> Unit)? = null
    private var onTopClickListener: (() -> Unit)? = null
    private var onBottomClickListener: (() -> Unit)? = null

    override fun onStart() {
        super.onStart()
        window?.apply {
            setLayout(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT
            )
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        dialogBinding = DialogLovelyBinding.inflate(layoutInflater, FrameLayout(context), false)
        setContentView(dialogBinding.root)
        setCanceledOnTouchOutside(false)
        setCancelable(false)

        dialogTitle?.let {
            dialogBinding.title.text = it
        }
        dialogMessage?.let {
            if(isSpan) {
                dialogBinding.content.movementMethod = LinkMovementMethod.getInstance()
                dialogBinding.content.setText(it,TextView.BufferType.SPANNABLE)
            } else {
                dialogBinding.content.text = it
            }
        }

        if (isOnlyPositiveButton) {
            dialogBinding.cancel.isVisible = false
        }
        dialogBinding.ivTopClose.isVisible = showTopClose
        dialogBinding.ivTopClose.clickable {
            if(onTopClickListener != null)
                onTopClickListener?.invoke()
            else
                dismiss()
        }
        dialogBinding.ivBottomClose.isVisible = showBottomClose
        dialogBinding.ivBottomClose.clickable {
            if(onBottomClickListener != null)
                onBottomClickListener?.invoke()
            else
                dismiss()
        }
        dialogBinding.ivTopImg.isVisible = showTopImage
        if(showTopImage) {
            dialogBinding.ivTopImg.setImageResource(topImage)
        }

        setupNegativeButton()
        setupPositiveButton()
    }

    private fun setupNegativeButton() {
        dialogBinding.cancel.text = negativeButtonText
        dialogBinding.cancel.helper.let {
            it.textColor = negativeButtonTextColor
            it.backgroundColor = negativeButtonColor
        }
        dialogBinding.cancel.clickable {
            negativeButtonClickListener?.invoke(this)
            dismiss()
        }
    }

    private fun setupPositiveButton() {
        dialogBinding.confirm.text = positiveButtonText
        dialogBinding.confirm.helper.let {
            it.textColor = positiveButtonTextColor
            it.backgroundColor = positiveButtonColor
        }
        dialogBinding.confirm.clickable {
            positiveButtonClickListener?.invoke(this)
            dismiss()
        }
    }

    override fun dismiss() {
        onDismissListener?.invoke()
        super.dismiss()
    }

    /**
     * 自定义对话框的建造者类。
     *
     * @property context 上下文对象
     */
    class Builder(private val context: Context) {
        private val customDialog = LovelyDialog(context)

        /**
         * 设置对话框的标题。
         *
         * @param title 对话框标题
         * @return Builder 实例
         */
        fun setDialogTitle(title: CharSequence): Builder {
            customDialog.dialogTitle = title
            return this
        }

        /**
         * 设置对话框的消息内容。
         *
         * @param message 对话框消息
         * @return Builder 实例
         */
        fun setDialogMessage(message: CharSequence, isSpan: Boolean = false): Builder {
            customDialog.isSpan = isSpan
            customDialog.dialogMessage = message
            return this
        }

        /**
         * 设置只显示确定按钮。
         *
         * @return Builder 实例
         */
        fun showOnlyPositiveButton(): Builder {
            customDialog.isOnlyPositiveButton = true
            return this
        }

        /**
         * 设置取消按钮的相关属性。
         *
         * @param text 取消按钮文本
         * @param textColor 取消按钮文本颜色
         * @param buttonColor 取消按钮背景颜色
         * @param clickListener 取消按钮点击监听器
         * @return Builder 实例
         */
        fun setNegativeButton(
            text: CharSequence = context.getString(R.string.cancel),
            textColor: Int = context.getColor(R.color.blue_7782A7),
            buttonColor: Int = "#D1D1FF".toColorInt(),
            clickListener: ((Dialog) -> Unit)? = null
        ): Builder {
            customDialog.negativeButtonText = text
            customDialog.negativeButtonTextColor = textColor
            customDialog.negativeButtonColor = buttonColor
            customDialog.negativeButtonClickListener = clickListener
            return this
        }

        fun setOnDismissListener(listener: () -> Unit): Builder {
            customDialog.onDismissListener = listener
            return this
        }

        fun dismiss() {
            customDialog.dismiss()
        }

        fun setTopClose(show: Boolean, clickListener: (() -> Unit)? = null): Builder {
            customDialog.showTopClose = show
            customDialog.onTopClickListener = clickListener
            return this
        }

        fun setBottomClose(show: Boolean, clickListener: (() -> Unit)? = null): Builder {
            customDialog.showBottomClose = show
            customDialog.onBottomClickListener = clickListener
            return this
        }

        fun setTopImg(show: Boolean, img: Int): Builder {
            customDialog.showTopImage = show
            customDialog.topImage = img
            return this
        }

        /**
         * 设置确定按钮的相关属性。
         *
         * @param text 确定按钮文本
         * @param textColor 确定按钮文本颜色
         * @param buttonColor 确定按钮背景颜色
         * @param clickListener 确定按钮点击监听器
         * @return Builder 实例
         */
        fun setPositiveButton(
            text: CharSequence = context.getString(R.string.confirm),
            textColor: Int = Color.WHITE,
            buttonColor: Int = context.getColor(R.color.color_main),
            clickListener: ((Dialog) -> Unit)? = null
        ): Builder {
            customDialog.positiveButtonText = text
            customDialog.positiveButtonTextColor = textColor
            customDialog.positiveButtonColor = buttonColor
            customDialog.positiveButtonClickListener = clickListener
            return this
        }

        /**
         * 构建并返回自定义对话框实例。
         *
         * @return 自定义对话框实例
         */
        fun build(): LovelyDialog {
            return customDialog
        }
    }
}